from django.views import View

from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequiredUser


class SetPwdView(LoginRequiredUser, View):
    """ 修改密码 """

    @Req.getJson
    def post(self, request):
        password = request.json_dict.get("password")
        newPassword = request.json_dict.get("newPassword")

        account = request.user
        if not account.check_password(password):
            return Res.Json(Res.Warn, msg="原密码错误")

        account.set_password(newPassword)
        account.save()
        return Res.Json(Res.OK)
